'' Octopilot -- RxTest.spin
'' (c) 2008 Ben Levitt
'' http://code.google.com/p/octopilot/
''
'' I used RxTest to determine the pattern of pulses that were sent by my radio receiver.
''
'' Makes a serial connection at 38400 baud and prints out the sequence of states of the first 36 states
'' of the first 6 pins, which are listening to the receiver. This should be enough to see the pattern
'' repeat a few times.
''
'' For example 1 0 8 0 2 0 32 0 16 0 4 0 (repeat) would mean that pins 1, 4, 2, 6, 5, 3 fired seperately
'' in that order. Whereas 63 62 30 14 10 2 0 (repeat) would mean that all 6 pins turn on at the same
'' time, and they then turn off one by one.
''
'' I use a Spektrum DX7 transmitting to an AR6100.
'' The servos pulsed one at a time such that first one servo pin is pulsing on, then none are on, then a
'' different servo pulses on and turns off, then the next, and so on until all 6 have pulsed, then it repeats.
''
'' My pattern is: 1 0 8 0 2 0 32 0 16 0 4 0 which maps to:
'' Throttle, Rudder, Aileron, Flap/Aux, Gear, Elevator
''
'' That said, this doesn't actually matter.  :)  I was just trying to figure out if the pulses happened
'' simultaneously or serially, but I ended up writing the SerialIO code so that it should work either way.


OBJ
  debug         : "FullDuplexSerial"


CON
  _CLKMODE        = XTAL1 + PLL16X
  _XINFREQ        = 5_000_000

  debugPinBase    = 30  ' 30-31
  watchPinsMask   = 63  ' Pins 0-5
  numStates       = 36
  CR              = 13

  
VAR
  ' GlobalVars
  long states[numStates]
  long lastState
  byte index



PUB Start
  debug.Start(debugPinBase+1, debugPinBase, 0, 38400)
  
  repeat
    index := 0
    lastState := 0
  
    dira[17]~~
    outa[17]~~
    waitcnt(cnt+clkfreq*2)
    outa[17]~
  
    repeat index from 0 to numStates-1
      waitpne(lastState, watchPinsMask, 0)
      states[index] := ina & watchPinsMask
      lastState := states[index]
     
    repeat index from 0 to numStates-1
      debug.dec(states[index])
      debug.tx(" ")
      debug.tx(" ")
      debug.tx(" ")
      if (states[index] == 0)
        debug.tx(CR)
     
    debug.rx
    debug.tx(CR)